#!/bin/bash
commit_id=`git rev-parse --short HEAD`
echo ${commit_id}

manifest_path="app/src/main/AndroidManifest.xml"

debug_log="DEBUG_LOGSET"

#official
OFFICIAL_DEBUG_LOG="close_debug_log"
#OFFICIAL_DEBUG_LOG="open_debug_log"

#develop
DEVELOP_DEBUG_LOG="open_debug_log"

#build official
function buildofficial(){
sed -i "s/git_commit_id/${commit_id}/g" app/build.gradle
sed -i "s/${debug_log}/${OFFICIAL_DEBUG_LOG}/g" ${manifest_path}
versionnumber=`grep "versionName" app/build.gradle`
versionnumber=${versionnumber#*\"}
versionnumber=${versionnumber%\"*}
./gradlew_unix clean
./gradlew_unix assembleRelease
sed -i "s/${commit_id}/git_commit_id/g" app/build.gradle
sed -i "s/${OFFICIAL_DEBUG_LOG}/${debug_log}/g" ${manifest_path}
mv app/build/outputs/apk/app-release.apk retrofit_official_${versionnumber}.apk
}

#build develop
function builddevelop(){
sed -i "s/git_commit_id/${commit_id}(Beta)/g" app/build.gradle
sed -i "s/${debug_log}/${DEVELOP_DEBUG_LOG}/g" ${manifest_path}
versionnumber=`grep "versionName" app/build.gradle`
versionnumber=${versionnumber#*\"}
versionnumber=${versionnumber%\"*}
./gradlew_unix clean
./gradlew_unix assembleRelease --info --debug
sed -i "s/${commit_id}(Beta)/git_commit_id/g" app/build.gradle
sed -i "s/${DEVELOP_DEBUG_LOG}/${debug_log}/g" ${manifest_path}
mv app/build/outputs/apk/app-release.apk retrofit_develop_${versionnumber}.apk
}

builddevelop
buildofficial